package com.zl.learning.webservice.client;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.junit.Test;

import javax.xml.namespace.QName;

public class TestClient2 {
    @Test
    public void test1() {

        try {
            String str = getWebServiceResult("http://localhost:8080/services/TestWebService?wsdl", "http://localhost:8080/webservice2/services/TestWebService", "getUsers", "", "");
            System.out.println(str);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void test2() {
        try {
            String str = getWebServiceResult("http://127.0.0.1:6789/hello/services?wsdl", "http://service/", "sayHello2", "zhansan");
            System.out.println(str);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void test3() {
        try {
            String str = getWebServiceResult("http://localhost:8080/services/HelloWorld?wsdl", "http://example/", "sayHelloWorldFrom", "zhansan");
            System.out.println(str);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void test4() {
        try {
            String str = getWebServiceResult("http://localhost:9000/HelloWorld/services?wsdl", "http://example/", "sayHelloWorldFrom", "zhansan");
            System.out.println(str);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String getWebServiceResult(String url, String targetNamespace, String method, String... args) throws Exception {
        String rtnXml = null;
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();

            call.setTargetEndpointAddress(url);
            call.setOperationName(new QName(targetNamespace, method));

            call.setUseSOAPAction(true);
            call.setSOAPActionURI(targetNamespace + method);

            rtnXml = (String) call.invoke(new Object[]{args});

        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtnXml;
    }
}
